#include "connection.h"


Connection::Connection(QString path1, QString path2)
{
    _path1 = path1;
    _path2 = path2;

    _dir1 = new QDir(_path1);
    _dir2 = new QDir(_path2);
}

Connection::Connection(const Connection& obj)
{
    _path1 = obj.getPath1();
    _path2 = obj.getPath2();
}

bool Connection::setPath1(QString path)
{
    _path1 = path;
    return true;
}

bool Connection::setPath2(QString path)
{
    _path2 = path;
    return true;
}

QString Connection::getPath1() const
{
    return _path1;
}

QString Connection::getPath2() const
{
    return _path2;
}

QByteArray Connection::getHashFromPath(QString absPath)
{
    QByteArray ar;

    QFile file(absPath);
    if(!file.open(QFile::ReadOnly)) return false;

    ar = file.readAll();

    file.close();

    return QCryptographicHash::hash(ar, QCryptographicHash::Md4);
}

QByteArray Connection::getHashFromDB(QString filepath)
{
    QFile file("program/hash.db");
    QByteArray hash;

    if(file.open(QFile::ReadOnly)) return false;

    QDataStream in(&file);
    QString path;

    while(true && !file.atEnd())
    {
        in >> path >> hash;

        if(path == filepath) break;
    }

    return hash;
}

bool Connection::hasUpdates() //TODO: Доделать эту функцию
{
    if(_path1 == "" || _path2 == "") return false;

    int count;

    QFileInfoList fileList1 = _dir1->entryInfoList(QDir::Files, QDir::Time);
    QFileInfoList fileList2 = _dir2->entryInfoList(QDir::Files, QDir::Time);

    if(fileList1.count() != fileList2.count()) return true;

    count = fileList1.count();

    for(int i = 0; i < count; i++)
    {
        QString path1, path2;

        path1 = fileList1.at(i).absoluteFilePath();
        path2 = fileList2.at(i).absoluteFilePath();

        if(getHashFromPath(path1) != getHashFromDB(path1))
    }

    return false;
}

bool Connection::update()
{
    if(_path1 == "" || _path2 == "") return false;


    return true;
}
